TARGET = app
OUTPUT_DIR = build
NCPUS = `cat /proc/cpuinfo |grep processor|wc -l`
CROSS_COMPILE	:=
OPT_LEVEL = -O3

CC  = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++



#MASTER 
MODULES += -DMASTER_MODULE
INCLUDE += -I./utils
VPATH += ./utils
MASTER_SRC += ./%.cpp
MASTER_OBJ += ./%.o
MASTER_SRCS += $(wildcard *.cpp)
MASTER_OBJS += $(patsubst %.cpp,%.o,$(MASTER_SRCS))

INCLUDE += -I./trigger
VPATH += trigger
TRIGGER_SRC += ./trigger/%.cpp
TRIGGER_OBJ += ./trigger/%.o
TRIGGER_SRCS += $(wildcard ./trigger/*.cpp)
TRIGGER_OBJS += $(patsubst %.cpp,%.o,$(TRIGGER_SRCS))

INCLUDE += -I./slot
VPATH += slot
SLOT_SRC += ./slot/%.cpp
SLOT_OBJ += ./slot/%.o
SLOT_SRCS += $(wildcard ./slot/*.cpp)
SLOT_OBJS += $(patsubst %.cpp,%.o,$(SLOT_SRCS))

#UTILS
INCLUDE += -I./utils
VPATH += ./utils
UTILS_SRC += ./utils/%.cpp
UTILS_OBJ += ./utils/%.o
UTILS_SRCS += $(wildcard ./utils/*.cpp)
UTILS_OBJS += $(patsubst %.cpp,%.o,$(UTILS_SRCS))

#V4L2
MODULES += -DV4L2
INCLUDE += -I./v4l2
VPATH += v4l2
V4L2_SRC += ./v4l2/%.cpp
V4L2_OBJ += ./v4l2/%.o
V4L2_SRCS += $(wildcard ./v4l2/*.cpp)
V4L2_OBJS += $(patsubst %.cpp,%.o,$(V4L2_SRCS))

#DISPLAY
MODULES += -DDISPLAY
INCLUDE += -I./display 
VPATH += display
DISPLAY_SRC += ./display/%.cpp
DISPLAY_OBJ += ./display/%.o
DISPLAY_SRCS += $(wildcard ./display/*.cpp)
DISPLAY_OBJS += $(patsubst %.cpp,%.o,$(DISPLAY_SRCS))

#opencv
MODULES += -DOPENCV_M
INCLUDE += -I/usr/local/include/opencv4/ -I./display/opencv/
VPATH += /usr/local/include/opencv4/ ./display/opencv/
OPENCV_SRC += ./display/opencv/%.cpp
OPENCV_OBJ += ./display/opencv/%.o
OPENCV_SRCS += $(wildcard ./display/opencv/*.cpp)
OPENCV_OBJS += $(patsubst %.cpp,%.o,$(OPENCV_SRCS))
OPENCV_LIB += -L/usr/local/lib -lopencv_core\
		   -L/usr/local/lib -lopencv_imgproc\
		   -L/usr/local/lib -lopencv_highgui\
		   -L/usr/local/lib -lopencv_videoio\
		   -L/usr/local/lib -lopencv_imgcodecs
# OPENCVLIB += -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs

# 获取当前目录下的.o 和 build目录下的.o文件
# OBJS = $(wildcard $(OUTPUT_DIR)/*.o)
# 获取所有目标文件的名称
# OBJS = $(patsubst %.cpp,$(OUTPUT_DIR)/%.o,$(SRC))

OBJS += $(MASTER_OBJS) $(TRIGGER_OBJS) $(SLOT_OBJS) $(UTILS_OBJS) $(V4L2_OBJS) $(DISPLAY_OBJS) $(OPENCV_OBJS)

# .o文件自动补全生成
LIB += -lpthread -lm
CXXFLAG += $(OPT_LEVEL)
CXXFLAG += -std=c++11 

all:$(OBJS)
	$(CXX) $(CXXFLAG) $^ -o $(TARGET) $(LIB) $(OPENCV_LIB);
	mv $^ $(OUTPUT_DIR)/;
$(MASTER_OBJ):$(MASTER_SRC)
	@$(CXX) -c $(INCLUDE) -o $@ $^ $(LIB) $(OPENCV_LIB);
$(TRIGGER_OBJ):$(TRIGGER_SRC)
	@$(CXX) -c $(INCLUDE) -o $@ $^ $(LIB);
$(SLOT_OBJ):$(SLOT_SRC)
	@$(CXX) -c $(INCLUDE) -o $@ $^ $(LIB);
$(UTILS_OBJ):$(UTILS_SRC)
	@$(CXX) -c $(INCLUDE) -o $@ $^ $(LIB);
$(V4L2_OBJ):$(V4L2_SRC)
	@$(CXX) -c $(INCLUDE) -o $@ $^ $(LIB) $(OPENCV_LIB);
$(DISPLAY_OBJ):$(DISPLAY_SRC)
	$(CXX) -c $(INCLUDE) -o $@ $^ $(LIB) $(OPENCV_LIB);
$(OPENCV_OBJ):$(OPENCV_SRC)
	@$(CXX) -c $(INCLUDE) -o $@ $^ $(LIB) $(OPENCV_LIB);

clean:
	rm -f *.o $(OUTPUT_DIR)/*.o
	rm -f $(TARGET)
